

function meetingCtrl($scope, $http, $interval, ngDialog, paFetchService) {
  'ngInject';
  // 查看 编辑
  this.check = true;

  this.meetingList = [];
  // 查询安全会议信息
  this.queryData = {};
  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: '',
  };
  // 更换页码时
  $scope.pageChange = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/querySecurityMeetingList`,
      angular.extend(this.queryData, this.paginationData)
        )
      .then((resp) => {
        this.meetingList = resp.data.data;
        this.paginationData.total = resp.data.total;
      });
  };
  // 查询
  this.safetyMeeting = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/querySecurityMeetingList`,
      angular.extend(this.queryData, this.paginationData)
        ).
      then((resp) => {
        this.meetingList = resp.data.data;
        this.paginationData.total = resp.data.total;
      });
  };
  
  // 根据条件查询
  this.search = (param) =>{
    if (!param.startTime.$invalid && param.endTime.$invalid) {
      layer.alert('请填写结束时间');
      return false;
    } else if (param.startTime.$invalid && !param.endTime.$invalid) {
      layer.alert('请填写开始时间');
      return false;
    } else if (param.startTime.$valid && param.endTime.$valid) {
      if (param.startTime.$viewValue < param.endTime.$viewValue) {
        this.safetyMeeting();
        return false;
      } else {
        layer.alert('开始时间应小于结束时间');
      }
    } else {
      this.safetyMeeting();
    }
  };

  //重置
  this.reset = () => {
    this.queryData = {};
  }

  // 新增会议
  this.addMeeting = () => {
     ngDialog.open({
      template: 'addMeeting',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'meetingAddition',
      scope: $scope,
      controller: function mettingDetailCtrl($http, $scope) {
        'ngInject';

        this.meetingAdditionList = {};
        // 重置
        this.reset = () => {
          this.meetingAdditionList = {};
        };

        // 上传文件
        // 保存上传文件返回token
        this.profilesToken = [];

        // 保存
        this.save = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }
          // 把文件上传返回token传入this.meetingAdditionList
          this.fileIds = [];
          this.profilesToken.forEach((item) => {
            this.fileIds.push(item.id);
          });
          this.meetingAdditionList.meetingDocuments = this.fileIds.join(',');

          let reqData = this.meetingAdditionList;
          $http({
            method: 'POST',
            url: `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/insertSecurityMeeting`,
            data: reqData
          }).then((resp) => {
            if (resp.data.success) {
              layer.msg(resp.data.message);             
              $scope.closeThisDialog();
              $scope.initPage();
            } else {
              layer.alert(resp.data.message);
            }
          });
        };

        // 初始化页面
        this.initPage = () => {
          // 获得项目部list
          paFetchService
                        .getOrgList()
                        .then((resp) => {
                          this.orgList = resp.data;
                        });
        }

        this.initPage();
      }
    });
  }

  // 删除会议信息
  this.deleteMeetingItem = (id) => {
    let _this = this;
    $scope.modalTitle = '删除安全会议';
    $scope.modalContent = '确认要删除该会议吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
    then((value) => {
      let reqData = [];
      reqData.push(id);
      $http({
        method: 'DELETE',
        url: `${SERVER_CONTEXT}/api/securityactivities/securityMeeting/deleteByPrimaryKey/${id}`
      }).
        then((data) => {
          layer.msg('删除信息成功');
          this.safetyMeeting();
        });
    }, (reason) => {});
  } 

  // 查看安全会议
  this.meetingDetailView = (param) => {
    ngDialog.open({
      template: 'meetingDetail',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'vm',
      controller: function mettingDetailCtrl($http, $scope) {
        'ngInject';

        this.queryMeeting = () => {
          $http.get(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/queryById/${param.securityMeetingId}`).
            then((resp) => {
              console.log(resp);             
              this.meetingList = resp.data;
              this.fileItemList = resp.data.fileBeanList;
          });
        }

        // 文件下载
        this.getDownloadSrc = (file) => {
          return `${HOST_URL}${SERVER_CONTEXT}/api/securityactivities/securityMeeting/download/${file.id}`;
        }

        // 文件预览
        this.preview = (archiveDetail) => {
          let url = $state.href('documentPreview', {
            id: archiveDetail.id,
          });
          window.open(url,'_blank');
        }

        // 初始化页面
        this.initPage = () => {
          // 获取列表详情
          this.queryMeeting();
        }
        this.initPage();       
      }
    });
  }

  // 编辑安全会议
  this.meetingDetailDialog = function(param) {
    ngDialog.open({
      template: 'meetingDetailList',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'meetingDetail',
      scope: $scope,
      controller: function mettingDetailCtrl($http, $scope, $timeout, paFetchService) {
        'ngInject';
        // 获取列表详情
        this.queryMeeting = () => {
          $http.get(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/queryById/${param.securityMeetingId}`).
            then((resp) => {
              this.editMeetingList = resp.data;
              this.fileItemList = resp.data.fileBeanList;
              if (!this.editMeetingList.fileBeanList) {
                this.editMeetingList.fileBeanList = [];
              }
          });
        }

        // 文件下载
        this.getDownloadSrc = (file) => {
          return `${SERVER_IP}/api/securityactivities/securityMeeting/download/${file.id}`;
        }

        // 保存修改信息
        this.save = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }

          let fileTokens = this.editMeetingList.fileBeanList;
          this.filesId = [];
          this.editMeetingList.fileBeanList.forEach((item) => {
            this.filesId.push(item.id);
          })
          this.editMeetingList.meetingDocuments = this.filesId.join(',');

          $http.post(`${SERVER_CONTEXT}/api/securityactivities/securityMeeting/updateSecurityMeeting`,
              this.editMeetingList).
            then((resp) => {
              if (resp.data.success) {
                layer.msg(resp.data.message);
                $scope.closeThisDialog();
                $scope.initPage();
              } else {
                layer.alert(resp.data.message);
              }
            });
        }

        // 初始化页面
        this.initPage = () => {
          // 获取列表详情
          this.queryMeeting();

          // 获得项目部list
          paFetchService.getOrgList().then((resp) => {
            this.orgList = resp.data;
          });
        }
        this.initPage();       
      }
    });
  }

  $scope.initPage = () => {
    // 获得项目部list
    paFetchService
                  .getOrgList()
                  .then((resp) => {
                    this.orgList = resp.data;
                  });

    // 获取安全会议列表
    this.safetyMeeting();
  }

  // 升序、降序
  this.up = (param,index) => {   
    this.queryData.columeName = param;
    this.queryData.order = index;
    this.safetyMeeting();
  }


  // 初始化页面
  $scope.initPage();
}

export default meetingCtrl;